package com.baijiayun.live.ui.pptpanel.handsuplist;

import android.app.Dialog;
import android.content.Context;
import androidx.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.baijiayun.live.ui.R;

public class PPTErrorDialog extends Dialog {

    private PPTErrorDialog(@NonNull Context context) {
        super(context);
    }

    public static class Builder{
        private Context context;
        private String descriptionText;
        private String suggestionText;
        private String negativeText;
        private String positiveText;
        private OnClickListener onPositiveClickListener;
        private OnClickListener onNegativeClickListener;

        public Builder(Context context){
            this.context = context;
        }

        public Builder setDescriptionText(String descriptionText){
            this.descriptionText = descriptionText;
            return this;
        }

        public Builder setSuggestionText(String suggestionText){
            this.suggestionText = suggestionText;
            return this;
        }

        public Builder setNegative(String negativeText, OnClickListener onNegativeClickListener){
            this.negativeText = negativeText;
            this.onNegativeClickListener = onNegativeClickListener;
            return this;
        }

        public Builder setPositive(String positiveText, OnClickListener onPositiveClickListener){
            this.positiveText = positiveText;
            this.onPositiveClickListener = onPositiveClickListener;
            return this;
        }

        public PPTErrorDialog create(){
            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final PPTErrorDialog dialog = new PPTErrorDialog(context);
            View layout = inflater.inflate(R.layout.bjy_dialog_anim_ppt_overtime, null);
            dialog.addContentView(layout, new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
            ((TextView) layout.findViewById(R.id.anim_ppt_error_description)).setText(descriptionText);
            ((TextView) layout.findViewById(R.id.anim_ppt_error_suggestion)).setText(suggestionText);
            if (TextUtils.isEmpty(negativeText)){
                layout.findViewById(R.id.anim_ppt_error_negative_btn).setVisibility(View.GONE);
            }else {
                ((TextView) layout.findViewById(R.id.anim_ppt_error_negative_btn)).setText(negativeText);
                if (onNegativeClickListener != null){
                    layout.findViewById(R.id.anim_ppt_error_negative_btn).setOnClickListener(v -> {
                        onNegativeClickListener.onClick(dialog, Dialog.BUTTON_NEGATIVE);
                        dialog.dismiss();
                    });
                }
            }
            if (TextUtils.isEmpty(positiveText)){
                layout.findViewById(R.id.anim_ppt_error_positive_btn).setVisibility(View.GONE);
            }else {
                ((TextView) layout.findViewById(R.id.anim_ppt_error_positive_btn)).setText(positiveText);
                if (onPositiveClickListener != null){
                    layout.findViewById(R.id.anim_ppt_error_positive_btn).setOnClickListener(v -> {
                        onPositiveClickListener.onClick(dialog, Dialog.BUTTON_POSITIVE);
                        dialog.dismiss();
                    });
                }
            }
            dialog.setContentView(layout);
            Window dialogWindow = dialog.getWindow();
            dialogWindow.setBackgroundDrawableResource(android.R.color.transparent);
            return dialog;
        }
    }
}
